﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Data.OleDb;
using System.Collections.Generic;


public partial class sendemail : System.Web.UI.Page
{

    public static string Semail = "";
    public static string Surl = "";
    public static string Stimeout = "60000";//默认发送时间间隔

    protected void Page_Load(object sender, EventArgs e)
    {
        Semail = "";
        Surl = "";

        cyly.AuthLogin();

        string OkQQ = cyly.GetUrlParaValues("qq");
        string email = cyly.GetUrlParaValues("email");

        string getCmd = "select * from setting where [SuserName]=@UserName";
        AccessDatabase ad = new AccessDatabase();
        OleDbDataReader reader = ad.ExeSql(getCmd, (new OleDbParameter[] { ad.createParamter("@UserName", cyly.getSystemLogin()) }));
        while (reader.Read())
        {
            Stimeout =Convert.ToString(Convert.ToInt32(reader["timeout"].ToString()) * 1000);
        }
        reader.Close();


        //页面过程
        if (!string.IsNullOrEmpty(email))
        {
            Response.Write("<div class='c_box_3' id='email_sendmsg'>" + sendemailOk(email) + "</div>");
            Response.Write("<script>setTimeout(function(){document.all['email_sendmsg'].innerHTML+='<div style=border-top:1px #fff solid;padding:3px; >正在准备发送下一封邮件,请稍候......</div>';},5000);</script>");
        }
        else
        {
            string url = Server.UrlDecode(cyly.GetUrlParaValues("url"));
            int page = Convert.ToInt32(cyly.GetUrlParaValues("page"));
            int isSendEmail = Convert.ToInt32(cyly.GetUrlParaValues("isSendEmail"));
            int isSaveData = Convert.ToInt32(cyly.GetUrlParaValues("isSaveData"));

            Surl = "getQQ.aspx?url=" + HttpContext.Current.Server.UrlEncode(url) + "&page=" + page + "&isSendEmail=" + isSendEmail + "&isSaveData=" + isSaveData + "";

            if (!string.IsNullOrEmpty(OkQQ))
            {
                string[] ArrQQ = cyly.SplitString(OkQQ, "|");
                for (int i = 0; i < ArrQQ.Length; i++)
                {
                    if (ArrQQ[i].Length > 3)
                        Semail += ArrQQ[i] + "@qq.com,";

                }
            }
            else
            {
                Response.Write("<div class='label2' id='email_sendmsg'>参数载入失败!</div>");
                Response.Write("<script>setTimeout(function(){document.all['email_sendmsg'].innerHTML='正在返回...';parent.location.href='" + Surl + "'},2000);</script>");

            }
        }


    }


    public static string sendemailOk(string Email)
    {

        if (!string.IsNullOrEmpty(Email))
        {
            Email = Email;
            string getCmd = "select * from setting where [SuserName]=@UserName";

            //取得邮件数据
            AccessDatabase ad = new AccessDatabase();
            OleDbDataReader reader = ad.ExeSql(getCmd, (new OleDbParameter[] { ad.createParamter("@UserName", cyly.getSystemLogin()) }));
            string subject = "";
            string body = "";
            string[] Asmtp = {""};
            string senderName = "";
            string sendMailType = "";
            string stringS="";
            while (reader.Read())
            {
                subject = reader["subject"].ToString();
                body = reader["body"].ToString();
                senderName = reader["senderName"].ToString();
                sendMailType = reader["sendMailType"].ToString();
                stringS=reader["stringS"].ToString();

                Asmtp = cyly.SplitString(reader["smtp1"].ToString() + "," + reader["smtp2"].ToString() + "," + reader["smtp3"].ToString() + "," + reader["smtp4"].ToString() + "," + reader["smtp5"].ToString(), ",");
            }
            reader.Close();

            //替换邮件内容中的宏变量
            body = body.Replace("{email}", Email);
            body = body.Replace("{date}", DateTime.Now.ToShortDateString());

            string[] ArrStringS = cyly.SplitString(stringS, ",");
            if (ArrStringS.Length >= 1)
            {
                subject = cyly.getVarStringS(subject, ArrStringS[0], "{string1}");
                body = cyly.getVarStringS(body, ArrStringS[0], "{string1}");
                senderName = cyly.getVarStringS(senderName, ArrStringS[0], "{string1}");
                body = cyly.getVarStringS(body, ArrStringS[0], "{string1}");
            }
            if (ArrStringS.Length >= 2)
            {
                subject = cyly.getVarStringS(subject, ArrStringS[1], "{string2}");
                body = cyly.getVarStringS(body, ArrStringS[1], "{string2}");
                senderName = cyly.getVarStringS(senderName, ArrStringS[1], "{string2}");
                body = cyly.getVarStringS(body, ArrStringS[1], "{string2}");
            }
            if (ArrStringS.Length >= 3)
            {
                subject = cyly.getVarStringS(subject, ArrStringS[2], "{string3}");
                body = cyly.getVarStringS(body, ArrStringS[2], "{string3}");
                senderName = cyly.getVarStringS(senderName, ArrStringS[2], "{string3}");
                body = cyly.getVarStringS(body, ArrStringS[2], "{string3}");
            }

            //检查数据
            if (string.IsNullOrEmpty(subject))
                return "<span class=s2>无效主题!请在设置中添加主题.</span>";
            if (string.IsNullOrEmpty(senderName))
                return "<span class=s2>无效发件人姓名!请在设置中清加发件人姓名.</span>";

            //随机选出一个可以使用的邮局
            string ServerTypes = "";
            if (!string.IsNullOrEmpty(cyly.SplitString(Asmtp[0].ToString(), "|")[0]))
                ServerTypes += "0";
            if (!string.IsNullOrEmpty(cyly.SplitString(Asmtp[1].ToString(), "|")[0]))
                ServerTypes += ",1";
            if (!string.IsNullOrEmpty(cyly.SplitString(Asmtp[2].ToString(), "|")[0]))
                ServerTypes += ",2";
            if (!string.IsNullOrEmpty(cyly.SplitString(Asmtp[3].ToString(), "|")[0]))
                ServerTypes += ",3";
            if (!string.IsNullOrEmpty(cyly.SplitString(Asmtp[4].ToString(), "|")[0]))
                ServerTypes += ",4";
            string[] AServerTypes = cyly.SplitString(ServerTypes, ",");
            int i = cyly.getRandomNum(AServerTypes.Length);

            //检查是否为默认邮局发送
            if (sendMailType == "1")
                i = 0;
            string[] OkSmtp = cyly.SplitString(Asmtp[Convert.ToInt32(AServerTypes[i])], "|");

            //取得发送邮箱
            string sender = "";
            if (cyly.SplitString(OkSmtp[1], "@").Length == 2)
                sender = OkSmtp[1];
            else
            {
                sender = OkSmtp[1];
                string[] ArrSmtpServer = cyly.SplitString(OkSmtp[0], ".");
                if (ArrSmtpServer.Length == 2)
                    sender += "@" + ArrSmtpServer[1];
                else
                    return "<span class=s2>无效Smtp地址格式!</span>";
            }
            string returnMsg = "<b>邮件信息：</b><br><li>邮件主题：" + subject + "</li><li>发件人 &nbsp&nbsp：" + senderName + "</li><li>发送邮箱：<b>" + OkSmtp[1] + "</b>  接收邮箱：<b color=#006600>" + Email + "</b> </li>";


            if (cyly.SendEmail(sender, senderName, Email, subject, body, OkSmtp))
            {
                //更新该号已发送邮件
                string updateSql = "update qq set isemail=1 where qq= '" + cyly.SplitString(Email, "@")[0] + "'";
                ad.ExeSql(updateSql);


                return returnMsg + "<li>发送状态：<b style='color:#549803'>发送成功!</b></li>";
            }
            else
                return returnMsg + "<li style='color:red;'>发送状态：<b style='color:#ff0000'>发送失败!</b></li>";


        }
        else
        {
            return "Email识别失败!";
        }
    
    
    
    
    }


}
